package com.spring.cn.config.cycle;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * @author liangd
 * date 2020-11-14 12:28
 * code bean后置处理器
 */
public class MyBeanPostProcessor implements BeanPostProcessor {
    /**
     * 初始化之前调用
     * 默认处理所有的bean
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessBeforeInitialization:" + bean + "," + beanName);
        //指定bean对象
        if ("carBean".equals(beanName)){
            //可以再此处对bean进行操作
            System.out.println("config...");
        }
        return bean;
    }

    /**
     * 初始化之后调用
     *
     */
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessAfterInitialization:" + bean + "," + beanName);
        //偷梁换柱，初始化之后修改bean的值
        CarBean carBean = new CarBean();
        carBean.setBrand("宝马");
        return carBean;
    }
}
